package com.example.task1.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.task1.bean.HospitalHomeBean;
import com.example.task1.bean.HospitalUserManagerBean;
import com.example.task1.bean.LogisticsOrderBean;
import com.example.task1.bean.ParkingCartBean;
import com.example.task1.bean.TakeAddressBean;
import com.example.task1.bean.TakeOrderBean;
import com.example.task1.bean.TakeoutHomeListBean;
import com.example.task1.bean.TrafficCartListBean;
import com.example.task1.bean.TrafficTheBean;
import com.example.task1.bean.UserInfo;
import com.example.task1.bean.VolunteerBean;
import com.example.task1.http.Api;
import com.example.task1.ui.AllService.takeout.TakeoutShopAdapter;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.ui.user.activity.LoginActivity;
import com.example.task1.util.ApiResp;
import com.example.task1.util.ApiService;
import com.example.task1.util.CallBack;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyApplication extends Application {

    public static String BASE_URL = "http://124.93.196.45:10001";

    //eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjA0NTJjYzExLWNlNmUtNDdhYi1iMmIzLTMzYzhkNTM2NjUxMSJ9.5wl9zcHvSSobvXRWd895Ms2GO5E4IFiUpR-dxQC1AM8BQ3-ucWl9dYmLGmwT-5Jh-nD4_PZar6ZtwYRQhKJ3lQ
    public static String TOKEN = "";

    public static ApiService mApiService;

    public static Context sContext;

    private static SharedPreferences sharedPreferences;

    public static UserInfo.UserDTO user;

    public static LogisticsOrderBean logisticsOrderBean;

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        sContext = this;
        sharedPreferences = getSharedPreferences("SmartCity", Context.MODE_PRIVATE);
        initToken();
    }

    private void initToken() {
        String token = sharedPreferences.getString("token", "");
        if (!TextUtils.isEmpty(token)) {
            getUserInfo(token);
        }
    }

    public static ApiService getApiService() {
        return mApiService;
    }


    public static void init() {
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(1, TimeUnit.SECONDS)
                .build();
        mApiService = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .client(client)
                .baseUrl(BASE_URL)
                .build().create(ApiService.class);
    }


    public static void glide(ImageView view, String url) {
        Glide.with(view.getContext()).load(BASE_URL + url).into(view);
    }

    public static void glide(ImageView view, int url) {
        Glide.with(view.getContext()).load(BASE_URL + url).into(view);
    }

    public static SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }

    public static void banner(Banner banner, List<String> date) {
        banner.setAdapter(new BannerImageAdapter<String>(date) {
            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, String integer, int i, int i1) {
                Glide.with(MyApplication.sContext).load(BASE_URL + integer).into(bannerImageHolder.imageView);
            }
        });
        banner.setIndicator(new CircleIndicator(MyApplication.sContext), true);
    }

    public static void show(String msg) {
        Toast.makeText(sContext, msg, Toast.LENGTH_SHORT).show();
    }

    public static boolean isLogin(Context context) {
        if (TextUtils.isEmpty(MyApplication.TOKEN)) {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            return true;
        }
        return false;
    }

    public static void intent(Context context, Class<?> activity) {
        Intent intent = new Intent(context, activity);
        context.startActivity(intent);
    }

    public static void intent(Context context, Class<?> activity, int id) {
        Intent intent = new Intent(context, activity);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    public static void getUserInfo(String token) {
        Call<ApiResp> apiRespCall = Api.api().get(token, "/prod-api/api/common/user/getInfo");
        Api.send(apiRespCall, UserInfo.class, new CallBack<UserInfo>() {
            @Override
            public void success(UserInfo data) {
                TOKEN = token;
                user = data.getUser();
            }

            @Override
            public void error(String msg) {
                SharedPreferences.Editor edit = getSharedPreferences().edit();
                edit.remove("token");
                edit.commit();
                show("登入已过期，请重新登入");
            }
        });
    }

    public static TakeoutHomeListBean.RowsDTO TakeoutHomeListDate;
    public static TakeoutShopAdapter takeoutShopAdapter;
    public static TakeAddressBean.DataDTO TakeoutAddressDate;
    public static TakeOrderBean.RowsBean TakeoutOrderDetailsDate;

    public static class Traffic {
        public static TrafficCartListBean.RowsDTO cartDate;
    }

    public static class Hospital {
        public static HospitalUserManagerBean.RowsDTO userDate;
        public static HospitalHomeBean.RowsDTO hospitalDate;
    }
    public static class Parking{
        public static ParkingCartBean.RowsDTO CartDate;
    }
    public static class Volunteer{
        public static VolunteerBean.RowsDTO date;
    }
}